﻿using Entity = Ruirun.Mes.Entities.WareHouse;
using IContractService = Ruirun.Mes.IMesService;
using InputDto = Ruirun.Mes.Dtos.WareHouseInputDto;
using OutputDto = Ruirun.Mes.Dtos.WareHouseOutputDto;

namespace Ruirun.Mes.Api.Areas.Admin.Controllers
{
    [Description("仓库信息")]
    [ModuleInfo(OrderNo = 1, Position = "Wms", PositionName = "仓储模块")]
    public class WareHouseController : AdminControllerBase
    {
        /// <summary>
        /// 获取 服务接口
        /// </summary>
        protected IContractService ContractService => ServiceProvider.GetService<IContractService>();

        /// <summary>
        /// 读取
        /// </summary>
        [HttpPost]
        [Description("读取")]
        [ModuleInfo]
        public PageData<OutputDto> Read(PageRequest request)
        {
            Check.NotNull(request, nameof(request));
            IFunction function = this.GetExecuteFunction();

            Expression<Func<Entity, bool>> predicate = FilterService.GetExpression<Entity>(request.FilterGroup);
            var page = CacheService.ToPageCache<Entity, OutputDto>(ContractService.WareHouses, predicate, request.PageCondition,
               function);

            return page.ToPageData();
        }

        /// <summary>
        /// 新增
        /// </summary>
        [HttpPost]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("新增")]
        [ModuleInfo]
        public async Task<AjaxResult> Create(InputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List<string> ids = new();
            List<OutputDto> outputDtos = new();
            foreach (var dto in dtos)
            {
                var result = await ContractService.CreateWareHouses(dto);
                if (!result.Succeeded)
                {
                    return result.ToAjaxResult();
                }
                ids.Add(dto.Id.ToString());
                outputDtos.Add(dto.MapTo<OutputDto>());
            }
            return AjaxResult.Success(outputDtos, $"{nameof(Entity)}“{ids.ExpandAndToString()}”创建成功");
        }

        /// <summary>
        /// 更新
        /// </summary>
        [HttpPost]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("更新")]
        [ModuleInfo]
        public async Task<AjaxResult> Update(InputDto[] dtos)
        {
            Check.NotNull(dtos, nameof(dtos));
            List<string> ids = new();
            List<OutputDto> outputDtos = new();
            foreach (var dto in dtos)
            {
                var result = await ContractService.UpdateWareHouses(dto);
                if (!result.Succeeded)
                {
                    return result.ToAjaxResult();
                }
                ids.Add(dto.Id.ToString());
                outputDtos.Add(dto.MapTo<OutputDto>());
            }
            return AjaxResult.Success(outputDtos, $"{nameof(Entity)}“{ids.ExpandAndToString()}”更新成功");
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        [DependOnFunction(nameof(Read))]
        [UnitOfWork]
        [Description("删除")]
        [ModuleInfo]
        public async Task<AjaxResult> Delete(Guid[] ids)
        {
            Check.NotNull(ids, nameof(ids));
            var result = await ContractService.DeleteWareHouses(ids);
            return result.ToAjaxResult();
        }

        [HttpPost]
        public object[] ReadNode()
        {
            var nodes = ContractService.WareHouses.Select(a => new
            {
                a.Id,
                a.Name,
            }).OrderBy(a => a.Name).ToArray();
            return nodes;
        }

        /// <summary>
        /// 读取树节点
        /// </summary>
        /// <returns>组织机构树页列表</returns>
        [HttpPost]
        [AllowAnonymous]
        [Description("读取树")]
        public AjaxResult GetTreeNodes()
        {
            var datas = ContractService.WareHouses.Include(a => a.WareHouseAreas).ThenInclude(b => b.WareHouseAreaPoses).Select(a => new
            {
                a.Id,
                a.Name,
                Children = a.WareHouseAreas.Select(b => new
                {
                    b.Id,
                    Name = b.AreaName,
                    Children = b.WareHouseAreaPoses.Select(c => new
                    {
                        c.Id,
                        Name = c.AreaPosName,
                    })
                })
            }).ToList();
            return AjaxResult.Success(datas);

        }

    }
}